﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace FYPDemo
{
    public partial class SearchList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string keyword = Session["keyword"].ToString();

                bindSearchVendorResult(keyword);
                bindSearchProductResult(keyword);
            }
        }

        protected void searchMemberGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            string keyword = Session["keyword"].ToString();
            this.SearchMemberGrid.PageIndex = e.NewPageIndex;
            bindSearchVendorResult(keyword);
        }

        protected void searchMemberGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            string name = SearchMemberGrid.SelectedDataKey.Value.ToString();

            Response.Redirect("partnerPage.aspx?username2=" +name);
        }

        protected void searchProductGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            string keyword = Session["keyword"].ToString();
            this.searchProductGrid.PageIndex = e.NewPageIndex;
            bindSearchProductResult(keyword);
        }

        protected void searchProductGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ProductID = searchProductGrid.SelectedDataKey.Value.ToString();

            string vendor = Common.getVendorByProductID(ProductID);

            Response.Redirect("ProductDetails.aspx?ProductID=" + ProductID);
        }

        protected void bindSearchVendorResult(string keyword)
        {
            DataTable dt1 = Common.searchVendor(keyword);

            if (dt1.Rows.Count > 0)
            {
                SearchMemberGrid.DataSource = dt1;
                SearchMemberGrid.DataBind();
            }

            else
            {
                MemberText.Style.Add("display", "block");
            }
        }

        protected void bindSearchProductResult(string keyword)
        {
            DataTable dt2 = Common.searchProduct(keyword);

            if (dt2.Rows.Count > 0)
            {
                searchProductGrid.DataSource = dt2;
                searchProductGrid.DataBind();
            }

            else
            {
                productText.Style.Add("display", "block");
            }
        }
    }
}